package com.backpackerxl.blog.service.impl;

import com.backpackerxl.blog.dao.TypeRepository;
import com.backpackerxl.blog.model.Type;
import com.backpackerxl.blog.service.TypeService;
import com.backpackerxl.blog.vomodel.BlogCount;
import com.backpackerxl.blog.vomodel.KeyWordsQuery;
import com.backpackerxl.blog.web.NotFoundException;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author by：Backpackersxl
 * Date: 2021/5/5/005
 * Time: 23:39
 **/
@Service
public class TypeServiceImp implements TypeService {

    private final TypeRepository typeRepository;

    public TypeServiceImp(TypeRepository typeRepository) {
        this.typeRepository = typeRepository;
    }

    /**
     * 保存
     *
     * @param type 类型
     * @return 类型
     */
    @Transactional
    @Override
    public Type saveType(Type type) {
        return typeRepository.save(type);
    }

    /**
     * 查询
     *
     * @param id 博客id
     * @return 类型
     */
    @Transactional
    @Override
    public Type getType(Long id) {
        return typeRepository.findById(id).orElse(null);
    }

    @Override
    public Type getTypeByName(String name) {
        return typeRepository.findByName(name);
    }

    /**
     * 分页查询
     *
     * @param pageable 分页器
     * @return 分页
     */
    @Transactional
    @Override
    public Page<Type> listType(Pageable pageable) {
        return typeRepository.findAll(pageable);
    }

    /**
     * 更新
     *
     * @param id   博客id
     * @param type 类型
     * @return 类型
     */
    @SneakyThrows
    @Transactional
    @Override
    public Type updateType(Long id, Type type) {
        Type t = typeRepository.findById(id).orElse(null);
        if (t == null) {
            throw new NotFoundException("不存在该类型");
        }
        BeanUtils.copyProperties(type, t);

        return typeRepository.save(t);
    }

    /**
     * 删除
     *
     * @param id blog id
     */
    @Transactional
    @Override
    public void deleteType(Long id) {
        typeRepository.deleteById(id);
    }

    @Override
    public List<Type> listType() {
        return typeRepository.findAll();
    }

    @Override
    public List<Type> listTypeTop(Integer size) {
        Sort sort = Sort.by(Sort.Direction.DESC, "blogs.size");
        Pageable pageable = PageRequest.of(0, size, sort);
        return typeRepository.findTop(pageable);
    }

    @Override
    public List<BlogCount> listTypeCount() {
        List<BlogCount> counts = listTypeTop(6).stream().map(o -> new BlogCount(o.getName(), o.getBlogs().size())).collect(Collectors.toList());
        System.out.println(counts);
        return null;
    }

    @Override
    public Page<Type> listTypes(Pageable pageable, KeyWordsQuery key) {
        return typeRepository.findAll((Specification<Type>) (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (!"".equals(key.getKeywords()) && key.getKeywords() != null) {
                predicates.add(criteriaBuilder.like(root.get("name"), "%" + key.getKeywords() + "%"));
            }
            query.where(predicates.toArray(new Predicate[0]));
            return null;
        }, pageable);
    }


}
